package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.AddressDao;
import com.pojo.Address;
import com.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    //注入addressDao
    @Autowired
    private AddressDao addressDao;
    //查询这个用户所有地址
    @Override
    public List<Address> findAllByUid(Integer uId) {
        //首先构造QueryWrapper来进行条件的添加
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",uId);//相当于where id=1
        return addressDao.selectList(wrapper);
    }
    //修改单个地址
    @Override
    public boolean update(Address address) {
        return addressDao.updateById(address)>0 ? true : false;
    }
    //删除
    @Override
    public boolean delete(Integer id) {
        return addressDao.deleteById(id)>0 ? true : false;
    }
    //添加
    @Override
    public boolean add(Address address) {
        return addressDao.insert(address)>0 ? true :false;
    }
    //修改默认地址
    @Override
    public boolean updatedefault(Integer uid,Integer id) {
        //首先构造QueryWrapper来进行条件的添加
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",uid);//相当于where id=1
        wrapper.eq("value",1);
        Address address = addressDao.selectOne(wrapper);
        if(address!=null){
            address.setValue(false);
            addressDao.updateById(address);
        }


        Address address1 = addressDao.selectById(id);
        address1.setValue(true);
        return addressDao.updateById(address1)>0 ? true : false;
    }

    @Override
    public boolean updateondefault(Integer id) {
        Address address = addressDao.selectById(id);
        address.setValue(false);
        return addressDao.updateById(address)>0 ? true : false;
    }

}